package route

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"qihuang/controller/admin"
	"qihuang/controller/front"
	"qihuang/db/redis"
	"qihuang/loger"
	"qihuang/middleware"
	"qihuang/protocol"
)

type App struct {
	engine *gin.Engine
}

func Default() *App {
	app := &App{}
	app.engine = gin.Default()
	return app
}

func (c *App) Run() {
	loger.Default()
	redis.Default()
	//c.SetAccessLog()
	c.SetCors()
	c.setFront()
	c.setAdmin()
	c.set404()
	//c.LoginCheck()
}

func (c *App) GetEngine() *gin.Engine {
	return c.engine
}
func (c *App) SetAccessLog() {
	c.engine.Use(middleware.AddAccessLog())
}
func (c *App) SetCors() {
	c.engine.Use(middleware.Cors())
}

//自定义中间件第1种定义方式
func LoginCheck(c *gin.Context) {
	authHeader := c.Request.Header.Get("Authorization")
	if authHeader == "" {
		c.JSON(http.StatusOK, gin.H{
			"status": 1,
			"msg":    "请求头中auth为空",
		})
		c.Abort()
		return
	}
	jwt := middleware.JWT{}
	_, err := jwt.ParseToken(authHeader)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"status": 1,
			"msg":    err,
		})
		c.Abort()
		return
	}
}

func (c *App) setFront() {
	articleFrontCtrl := front.Articles{}
	userFrontCtrl := front.Users{}
	articleCommentCtrl := front.ArticleComment{}
	careCtrl := front.Care{}
	kinCtrl := front.Kin{}
	//limiter, err := middleware.NewLimiter("redis://localhost:6379")
	//if err != nil {
	//	log.Fatal(err)
	//}
	hospitalCtrl := front.Hospitals{}
	familyCtrl := front.Families{}
	c.engine.GET("/articles", articleFrontCtrl.GetList)
	c.engine.GET("/article/:id", articleFrontCtrl.Show)
	c.engine.GET("/comments", articleCommentCtrl.GetList)
	c.engine.GET("/hospitals", hospitalCtrl.GetList)
	c.engine.GET("/city/hospitals", hospitalCtrl.GetListByCity)
	c.engine.GET("/hospital/:id", hospitalCtrl.Show)
	c.engine.POST("/comment/add", LoginCheck, articleCommentCtrl.CreateComment)
	c.engine.GET("/categories", LoginCheck, articleFrontCtrl.GetCategories)
	c.engine.POST("/user/login/", userFrontCtrl.Login)
	c.engine.POST("/user/register/", userFrontCtrl.Register)
	c.engine.GET("/user/code/", userFrontCtrl.SendCode)
	c.engine.POST("/family/create/", familyCtrl.Create)
	c.engine.GET("/family/show/", familyCtrl.Show)
	c.engine.POST("/family/update/", familyCtrl.Update)
	c.engine.GET("/family/list/", familyCtrl.GetList)
	c.engine.GET("/family/delete/", familyCtrl.Delete)
	c.engine.GET("/cares/list/", careCtrl.GetList)
	c.engine.GET("/care/:id", careCtrl.Show)
	c.engine.GET("/kins/list/", kinCtrl.GetList)
	c.engine.GET("/kin/:id", kinCtrl.Show)
}

func (c *App) setAdmin() {
	//后台管理
	loginAdminCtrl := admin.Login{}
	articleAdminCtrl := admin.Articles{}
	userAdminCtrl := admin.User{}
	c.engine.POST("/adapi/login", loginAdminCtrl.Login)
	authorized := c.engine.Group("/adapi")
	authorized.Use(middleware.CheckAuth())
	{
		authorized.POST("/article", articleAdminCtrl.Add)
		authorized.PUT("/article/:id", articleAdminCtrl.Update)
		authorized.GET("/articles", articleAdminCtrl.GetList)
		authorized.DELETE("/article/:id", articleAdminCtrl.Delete)
		authorized.GET("/article/:id", articleAdminCtrl.Show)
		authorized.POST("/article/cache", articleAdminCtrl.DeleteCache)
		authorized.GET("/user", userAdminCtrl.Show)
	}
}

func (c *App) set404() {
	c.engine.NoRoute(func(context *gin.Context) {
		resp := protocol.Resp{Status: 404, Msg: "page not exists!", Data: ""}
		//返回404状态码
		context.JSON(http.StatusNotFound, resp)
	})
}
